"""
return uninterpreted WHIFF src for cgi argument PAGE
"""

from whiff.middleware import misc
from whiff import resolver
from whiff import whiffenv
from whiff.middleware import whiffColorize
import os

class whiffSrc(misc.utility):
    def __init__(self):
        pass
    def __call__(self, env, start_response):
        env = resolver.process_cgi(env, parse_cgi=True)
        path = whiffenv.cgiGet(env, "PAGE")
        if path.find('..')>=0 or path.find('//')>=0:
            raise ValueError, "sorry '..' and '//' not allowed "+repr(path)
        if not path.startswith("./"):
            raise ValueError, "path must start './'"
        path = path[2:]
        [directory, myfile] = os.path.split(__file__)
        fullpath = os.path.join(directory, path)+".whiff"
        #pr "joined", (directory, path, fullpath)
        txt = open(fullpath).read()
        app = whiffColorize.__middleware__(page=txt, lexer="html")
        return self.deliver_page(app, env, start_response)

__middleware__ = whiffSrc
